package com.shareach.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.shareach.common.BaseQueryOrder;
import com.shareach.dao.mongo.BaseBankCardDao;
import com.shareach.domain.BaseBankCardDomain;
import com.shareach.service.BaseBankCardService;
import com.shareach.service.exception.BaseService;
import com.shareach.util.ReflectionUtil;

@Service
public class BaseBankCardServiceImpl extends BaseService<BaseBankCardDomain> implements BaseBankCardService {

	@Autowired
	private BaseBankCardDao baseBankCardDao;

	@Override
	public Long save(BaseBankCardDomain domain) {
		return baseBankCardDao.insert(domain);
	}

	@Override
	public void del(BaseBankCardDomain domain) {
		if (domain.getId() == null)
			return;
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(domain.getId()));
		baseBankCardDao.delete(query, BaseBankCardDomain.class);
	}

	@Override
	public void update(BaseBankCardDomain domain) {

		Criteria.where("_id").is(domain);
		baseBankCardDao.update(Criteria.where("_id").is(domain.getId()), Update.update("name", domain.getName()));

	}

	@Override
	public BaseBankCardDomain getById(Long id) {
		if (id == null)
			return null;
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(id));
		BaseBankCardDomain baseBankCardDomain = baseBankCardDao.findOne(query);
		return baseBankCardDomain;
	}

	@Override
	public List<BaseBankCardDomain> getList(BaseBankCardDomain domain) {
		Map<Object, Object> domainMap = ReflectionUtil.parseDomaintoMap(domain);
		Query query = new Query();
		for (Map.Entry<Object, Object> entry : domainMap.entrySet()) {
			new Criteria();
			query.addCriteria(Criteria.where(entry.getKey().toString()).ne("null").and(entry.getKey().toString()).is(entry.getValue()));
		}
		List<BaseBankCardDomain> basebankCardList = baseBankCardDao.findList(query);
		return basebankCardList;
	}

	@Override
	public List<BaseBankCardDomain> getListByCriteria(Criteria criteria) {
		return baseBankCardDao.findList(criteria);
	}

	@Override
	public Page<BaseBankCardDomain> getPageResult(BaseBankCardDomain domain, BaseQueryOrder order) {
		Query query = parseDomaintoQuery(domain);
		Page<BaseBankCardDomain> domainPage = baseBankCardDao.getPageResult(query, order);
		return domainPage;
	}

}
